<?php
global $_W, $_GPC;
$uniacid = $_W['uniacid'];
$config = $this->module['config'];
$op = empty($_GPC['op']) ? 'bindwx' : $_GPC['op'];
$oauth2 = new Wxapi($config['h5_appid'], $config['h5_appsecret']);
$uid = $_GPC['uid'];
if ($op == 'wxlogin') {
    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&op=getcode';
    $re = $oauth2->getCode($url, 'snsapi_userinfo');
} elseif ($op == 'getcode') {
    $code = $_GPC['code'];
    //header("location: http://" . $_SERVER['HTTP_HOST'] . "/addons/goouc_class/vue/index.html?i=".$uniacid."#/login?code=" . $code);
    header("location: http://192.168.1.21:8080/addons/goouc_class/vue/index.html?i=" . $uniacid . "#/wxlogin?code=" . $code);
    exit;
} elseif ($op == 'getuserinfo') {
    $code = $_GPC['code'];
    if (!empty($code)) {
        $resultArray = $oauth2->getUsertoken($code);
        //通过access_token 获取用户详细信息  绑定微信开放平台后会出现 unionid的值
        $userinfo = $oauth2->getUserinfo($resultArray['access_token'], $resultArray['openid']);
        if(!$uid){
            //登录
            $has = pdo_get($this->t_user, array('id' => $uid, 'weid' => $_W['uniacid'], 'h5_openid' => $userinfo['openid']));
            if($has){
                //说明绑定成功
                //给用户生成token验证值
                $token = $this->createtoken();
                $dat = [
                    'weid' => $_W['uniacid'],
                    'uid' => $has['id'],
                    'token' => $token,
                    'createtime' => time()
                ];
                $has_token = pdo_get($this->t_user_token, ['weid' => $_W['uniacid'], 'uid' => $has['id']]);
                if ($has_token) {
                    $ress = pdo_update($this->t_user_token, $dat, array('id' => $has_token['id']));
                } else {
                    //插入user_token数据表
                    $ress = pdo_insert($this->t_user_token, $dat);
                }
                if (!$ress) {
                    return $this->getresult(0, '生成token失败了,请重新登录', ['code' => 4001]);
                }
                return $this->getresult(0, '用户基本信息', ['uid' => $has['id'], 'code' => 200, 'token' => $token]);

            }else{

            }

        }else{
            //uid存在绑定微信
            //检测
            $data = [
                'weid' => $_W['uniacid'],
                'h5_openid' => $userinfo['openid'],
                'nickname' => $userinfo['nickname'],
                'headimg' => $userinfo['headimgurl'],
            ];
            $res = pdo_update($this->t_user, $data,array('id'=>$uid));

        }

        if ($has) {
//            if (empty($has['h5_openid'])) {
//                $data['h5_openid'] = $userinfo['openid'];
//                pdo_update($this->t_user, $data, ['id' => $has['id']]);
//            }

            //判断一下有没有unionId没有的话更新一下

        } else {
            $data = [
                'weid' => $_W['uniacid'],
                'h5_openid' => $userinfo['openid'],
                'nickname' => $userinfo['nickname'],
                'headimg' => $userinfo['headimgurl'],
                //'unionId' => $userinfo['unionid'],
                'createtime' => time(),
            ];
            $res = pdo_insert($this->t_user, $data);
            if ($res) {
                $uid = pdo_insertid();
                //给用户生成token验证值
                $token = $this->createtoken();
                $dat = [
                    'weid' => $_W['uniacid'],
                    'uid' => $uid,
                    'token' => $token,
                    'createtime' => time()
                ];
                //插入user_token数据表
                $ress = pdo_insert($this->t_user_token, $dat);
                if (!$ress) {
                    return $this->getresult(0, '生成token失败了,请重新登录', ['code' => 4001, 'uid' => $uid]);
                }
                return $this->getresult(0, '返回用户id以及token', ['uid' => $uid, 'code' => 200, 'token' => $token, 'userinfo' => $userinfo, 'codes' => $resultArray]);
            } else {
                return $this->getresult(0, '看数据', ['msg' => '新用户注册失败', 'code' => 4002]);
            }
        }
    }
}


